import { Pipe, PipeTransform } from '@angular/core';

/**
 * 四位分割
 */
@Pipe({
  name: 'segment',
})
export class SegmentPipe implements PipeTransform {
  /**
   * @Input: {{'6235425252131257' | segment}}
   * @Output: 6235 6235 6235 1257
   */
  transform(value: string) {
    if(!value) return value;
    const oldArr= value.split('');
    const newArr= oldArr.filter(item=> item.trim().length>0);
    let index= 0;
    const arr= newArr.map(item=>{
      index++;
      if(index === 4){
        index= 0;
        item+= ' ';
      }
      return item;
    });
    return arr.join('');
  }
}
